﻿using AutoMapper;
using HealthcareSystem.API.Read.Application.Command.ChronicSign;
using HealthcareSystem.API.Read.DTO;
using HealthcareSystem.Domain;
using MediatR;
using System.ComponentModel;
using System.Runtime;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Read.Application.CommandHandel.ChronicSignHandel
{
    public class ShowCsignCommadHandel : IRequestHandler<ShowCsignCommad, ApiResult<ApiPaging<List<CSignDTO>>>>
    {
        private readonly IBaseRepository<ChronicSign> cSignRepository;
        private readonly IBaseRepository<Record> recordRepository;

        public ShowCsignCommadHandel(IBaseRepository<ChronicSign> CSignRepository,IBaseRepository<Record> recordRepository)
        {
            cSignRepository = CSignRepository;
            this.recordRepository = recordRepository;
        }

        public Task<ApiResult<ApiPaging<List<CSignDTO>>>> Handle(ShowCsignCommad request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<CSignDTO>>> result = new ApiResult<ApiPaging<List<CSignDTO>>>();
            result.Code = ResultCode.Success;
            result.Message = ResultCode.查询成功.ToString();

            var list = from a in cSignRepository.GetAll()
                       join b in recordRepository.GetAll() on a.RecordId equals b.RecordId
                       where a.CSignIsdel==false 
                       select new CSignDTO
                       {
                           //签约
                           CSignId = a.CSignId,
                           CSignTeam = a.CSignTeam,
                           CSignDoctor = a.CSignDoctor,
                           CSignBegin = a.CSignBegin,
                           CSignEnd = a.CSignEnd,
                           CSignManage = a.CSignManage,
                           CSignLevel = a.CSignLevel,
                           CSignImage = a.CSignImage,
                           CSignIsdel = a.CSignIsdel,

                           //档案
                           RecordId = b.RecordId,
                           RecordName = b.RecordName,
                           RecordSex = b.RecordSex,
                           RecordAge = b.RecordAge,
                           RecordIdcard = b.RecordIdcard,
                           RecordPhone = b.RecordPhone,
                           RecordSite = b.RecordSite,
                           RecordTeam = b.RecordTeam,
                           RecordState = b.RecordState,
                           RecordDate = b.RecordDate,
                           RecordMarriage = b.RecordMarriage,
                           RecordBlood = b.RecordBlood,
                           RecordInsured = b.RecordInsured,
                           RecordProfession = b.RecordProfession,
                           RecordCulture = b.RecordCulture
                       };  
            if (!string.IsNullOrEmpty(request.Name))
            {
                list = list.Where(p => p.RecordName.Contains(request.Name));
            }
            if (request.BeginTime!=null)
            {
                var begintime = request.BeginTime;
                list = list.Where(p=>p.CSignBegin >= begintime);
            }
            if (request.EndTime!=null)
            {
                var endtime = request.EndTime;
                list = list.Where(p => p.CSignBegin <= endtime);
            }
            if (!string.IsNullOrEmpty(request.CSignTeam))
            {
                list = list.Where(p => p.CSignTeam.Contains(request.CSignTeam));
            }

            ApiPaging<List<CSignDTO>> paging = new ApiPaging<List<CSignDTO>>()
            {
                totalCount = list.Count(),
                totalPage =(int)Math.Ceiling(list.Count()*1.0/request.Pagesize),
                Data = list.OrderByDescending(p => p.CSignId).Skip((request.Pageindex - 1) * request.Pagesize).Take(request.Pagesize).ToList(),
            };

            result.Data = paging;
              return Task.FromResult(result);
        }
    }
}
